package service

import (
	"errors"
	"fmt"
	"github.com/go-admin-team/go-admin-core/sdk/config"
	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"
	"tuowei.com/app/api/models"
	"tuowei.com/app/api/service/dto"
)

type ApiCourse struct {
	service.Service
}

func (e *ApiCourse) Get(c *dto.SysCourseGetById, data *models.ApiCourse) error {
	var model models.ApiCourse
	err := e.Orm.Debug().Model(model).Where("course_id = ?", c.CourseId).First(&data).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetSysConfigPage error:%s", err)
		return err
	}
	return nil
}

// getList 获取课程章节
func (e *ApiCourse) getList(linkId int, list *[]models.SysCourseWare, _type string) error {
	var err error
	var data models.SysCourseWare

	db := e.Orm.Model(&data).
		Where("course_id = ?", linkId)

	if _type == "course" {
		db = db.Where("course_ware <> ?", "")
	} else if _type == "task" {
		db = db.Where("is_contain_exercise  = ?", 1)
	}

	err = db.Find(&list).Error
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

func setWatchKey(studentId, courseId, chapterId int) string {
	return fmt.Sprintf("sys_course_watch_progres_%d_%d_%d", studentId, courseId, chapterId)
}

func getWatchProgress(studentId, courseId, chapterId int) (int, error) {
	key := setWatchKey(studentId, courseId, chapterId)
	return config.GetRedisClient().Get(key).Int()
}

// Call 递归构造组织数据
func (e *ApiCourse) CourseWareTreeCall(linkId int, _type string, studentId ...int) (m []models.CourseWareTree, err error) {
	var list []models.SysCourseWare
	err = e.getList(linkId, &list, _type)
	m = make([]models.CourseWareTree, 0)
	for i := 0; i < len(list); i++ {
		if _type == "course" {
			if list[i].Pid != 0 {
				continue
			}
		} else {
			if list[i].Pid == 0 {
				continue
			}
		}

		s := models.CourseWareTree{}
		s.Id = list[i].Id
		s.Name = list[i].Name
		s.CourseId = list[i].CourseId
		s.IsShow = list[i].IsShow
		s.Url = list[i].CourseWare
		s.IsContainExercise = list[i].IsContainExercise
		var stuId int
		if len(studentId) > 0 {
			stuId = studentId[0]
		}
		deptsInfo := e.courseWareTreeCall(&list, s, stuId)
		m = append(m, deptsInfo)
	}
	return
}

// Call 递归构造组织数据
func (e *ApiCourse) courseWareTreeCall(deptList *[]models.SysCourseWare, result models.CourseWareTree, studentId int) models.CourseWareTree {
	list := *deptList
	min := make([]models.CourseWareTree, 0)
	for j := 0; j < len(list); j++ {
		if result.Id != list[j].Pid {
			continue
		}
		mi := models.CourseWareTree{Id: list[j].Id, Name: list[j].Name, Url: list[j].CourseWare, UrlName: list[j].UrlName, IsShow: list[j].IsShow, IsContainExercise: list[j].IsContainExercise, Pid: list[j].Pid, CourseId: list[j].CourseId}
		ms := e.courseWareTreeCall(deptList, mi, studentId)
		if studentId > 0 {
			ms.WatchDuration, _ = getWatchProgress(studentId, mi.CourseId, mi.Id)
		}
		min = append(min, ms)
	}
	result.Children = min
	return result
}
